$(function() {
    
    var reUserName = /^[0-9a-zA-Z]{4,15}$/; //只输入数字和字母的正则
    var rePass     = /^[0-9a-zA-Z]{6,20}$/; //只输入数字和字母的正则
    var reName     = /^[\u4e00-\u9fa5]{2,10}$/;//只输入汉字的正则
    var reMail     = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; // mail
    var reTel      = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
    var reMobile   = /^([0-9]{11})$/; //只输入数字和字母的正则
    var reQQ       = /^([0-9]{4,12})$/; //只输入数字和字母的正则
    
    /**
     * 学员管理 js对象
     */
    stuMngEvent = {
        
        ids : new Array(),
        names : new Array(),
        
        ids_new : new Array(),
        names_new : new Array(),
        
        courids : new Array(),
        cournames : new Array(),
        
        courids_new : new Array(),
        cournames_new : new Array(),
            
        init:function () {
            
           this.addStuEvent();
           this.setCourEvent();
           this.searchListEvent();
          
        },
        
        searchListEvent : function (){
        	 $('#searchClass').click(function(){
        		 
        		var keyword = $.trim($('#searchKeyId').val());// 查询关键字
        		var createTimeId1 = $.trim($('#createTimeId1').val());// 开始时间
        		var createTimeId2 = $.trim($('#createTimeId2').val());// 结束时间
        		if(('' != createTimeId1 && '' == createTimeId2) || ('' == createTimeId1 && '' != createTimeId2)){
        			alert('帐号创建时间不能为空');
        			return;
        		}
        		if('' != createTimeId1 && '' != createTimeId2){
        			
        			var st = createTimeId1.split("-");    
        			var starttime =new Date(st[0],st[1],st[2]);    
        			var starttimes = starttime.getTime();
        			
        			var ed = createTimeId2.split("-");    
        			var edtime =new Date(ed[0],ed[1],ed[2]);    
        			var edtimes = edtime.getTime();
        			if(starttimes > edtimes) {
        				alert('帐号创建时间有误,请重新选择!');
        				return;
        			}
        		}
        		 
     			$.ajax({
     	  			  type: "POST",
     	  			  url: contextRootPath + "/stuControl/stuMngList",
     	  			  data:JSON.stringify({"searchKey": keyword, "createTimeBegin":createTimeId1,"createTimeEnd": createTimeId2}),
     	  			  dataType: "html",
     	  			  contentType: "application/json; charset=utf-8",
     	  			  success: function(data){
     	  				  /** 成功构造内容**/
     	  				  $("#contentdiv").html(data);
     	  			 	  if('' != keyword) {
     	  			 		$('#searchKeyId').val(keyword);
     	  			 	  }
     	  			 	  if('' != createTimeId1) {
     	  			 		$('#createTimeId1').val(createTimeId1);
     	  			 	  }
     	  			 	  if('' != createTimeId2) {
     	  			 		$('#createTimeId2').val(createTimeId2);
     	  			 	  }
     	  				  stuMngEvent.init();
     	  				} 
     				});
        	 });
        	
        },
        
        /**
         * 添加学员按钮
         */
        addStuEvent : function (){
            $('#addStu').click(function(){
                $.ajax({
                    type: "POST",
                    url: contextRootPath + "/stuControl/stuAddInit",
                    contentType: "application/json; charset=utf-8",  
                    dataType: "html",
                    success: function(data){
                          /** 成功构造内容**/
                        $("#contentdiv").html(data);
                        stuMngEvent.saveEvent();
                        stuMngEvent.backEvent();
                    }
                });
                  
          });
        },
        
        /**
         * 分配课程
         */
        setCourEvent : function (){
            $('#setCour').click(function(){
                $.ajax({
                    type: "POST",
                    url: contextRootPath + "/stuControl/stuCourInit",
                    contentType: "application/json; charset=utf-8",  
                    dataType: "html",
                    success: function(data){
                          /** 成功构造内容**/
                        $("#contentdiv").html(data);
                             stuMngEvent.changeCourEvent();
                             stuMngEvent.changeUserEvent();
                    }
                });
                  
          });
        },
        
        /**
         * 选择课程事件
         */
        changeCourEvent : function (){
            $('#xzkcID').click(function(){
                
                $.ajax({
                    type: "get",
                    url: contextRootPath + "/stuControl/popCourInit",
                    contentType: "application/json; charset=utf-8",  
                    dataType: "html",
                    success: function(data){
                          /** 成功构造内容**/
                        $("#select01").html(data);
                        $('#yxzkcID div').empty();
                        $('#select01').show();
                        stuMngEvent.courids = new Array();
                        stuMngEvent.cournames = new Array();
                    }
                });
                
                
          });
        },
        
        
        /**
         * 选择用户事件
         */
        changeUserEvent : function (){
            $('#xzyhID').click(function(){
                //$('#select02').show();
                $.ajax({
                    type: "get",
                    url: contextRootPath + "/stuControl/popStuInit",
                    contentType: "application/json; charset=utf-8",  
                    dataType: "html",
                    success: function(data){
                          /** 成功构造内容**/
                        $("#select02").html(data);
                        $("#xzUserIdList").empty();
                        $('#select02').show();
                        stuMngEvent.ids = new Array();
                        stuMngEvent.names = new Array();
                    }
                });
          });
        },
        
        /**
         * 选择用户事件
         */
        searchUser : function (){
                //$('#select02').show();
                
                if('' == $.trim($('#userNameId').val())) {
                    alert('请输入用户名！');
                    return;
                } 
                $.ajax({
                    type: "get",
                    url: contextRootPath + "/stuControl/popStuInit2",
                    //contentType: "application/json; charset=utf-8",
                    //data:JSON.stringify({"name": $.trim($('#userNameId').val())}),
                    data:{"name": $.trim($('#userNameId').val())},
                    dataType: "html",
                    success: function(data){
                          /** 成功构造内容**/
                        $("#viewuserlist").html(data);
                        $("#xzUserIdList").empty();
                        $('#select02').show();
                        stuMngEvent.ids = new Array();
                        stuMngEvent.names = new Array();
                    }
                });
        },
        
        
        
        /**
         * 点击用户图片
         * @param thisObj
         */
        getUser : function (thisObj) {
            var userid = $(thisObj).parent('div').next('.tab5010102').find('a').attr('class');;
            
            var usernm = $(thisObj).parent('div').next('.tab5010102').find('p').text();
            
            for (var i = 0; i < stuMngEvent.ids.length;i++) {
                if(userid == stuMngEvent.ids[i]) {
                    return;
                }
            }
            stuMngEvent.ids.push(userid);
            stuMngEvent.names.push(usernm);
            var ht = '<span>'+ usernm + '<a href="#" onclick="stuMngEvent.clear(this);" id="'+userid+'" ><img src="../images/cha.png"/></a>&nbsp;&nbsp;</span>';
            $('#xzUserIdList').append(ht);
        },
        
        
        
        /**
         * 选择用户后 ,点击删除事件
         * @param thisObj
         */
        clear : function (thisObj) {
            for (var i = 0; i < stuMngEvent.ids.length;i++) {
                if($(thisObj).attr('id') == stuMngEvent.ids[i]) {
                    stuMngEvent.ids.splice(i,1);
                    stuMngEvent.names.splice(i,1);
                     break;
                }
            }
            $(thisObj).parent('span').detach();
        },
        
        /**
         * 选择用户确实点击
         */
        chooseUser: function(){
            
            $('#select02').hide();
            if(null != stuMngEvent.ids && stuMngEvent.ids.length > 0 && null != stuMngEvent.ids[0] && "" != stuMngEvent.ids[0] ){
                var ht = '';
                for (var i = 0; i < stuMngEvent.ids.length; i++) {
                    ht += stuMngEvent.names[i] + '&nbsp;&nbsp;<a href="#" onclick="stuMngEvent.clearStuMain(this);" id="'+stuMngEvent.ids[i]+'"><img src="../images/cha.png" /></a>&nbsp;&nbsp;';
                }
                stuMngEvent.ids_new = stuMngEvent.ids;
                stuMngEvent.names_new = stuMngEvent.names;
                $('#namelistID').append(ht);
            }
        },
        // 返回按钮
        backEvent: function () {
        	  $('#stu01 .agen_adst03 a:eq(1)').click(function(){
        		  $.ajax({
        			  type: "POST",
        			  url: contextRootPath + "/agent/htmlcontent",
        			  data:JSON.stringify({"type": 'agnav05',"articleTp": "1"}),
        			  dataType: "html",
        			  contentType: "application/json; charset=utf-8",
        			  success: function(data){
        				  /** 成功构造内容**/
        				  $("#contentdiv").html(data);
        				  stuMngEvent.init();
        				} 
        			});
        		
        	  });
        },
      
        /**
         * 保存学员
         */
        saveEvent: function () {
            
              $('#addsure').click(function(){
                  
                 var userLnm = $('#userLnm').val();//用户名
                var userPass = $('#userPass').val();//密码
                var userRnm = $('#userRnm').val();//学员姓名
                var userEmail = $('#userEmail').val();//注册邮箱
                var userMale = $('#userLnm').val();//性别
                var userEdu = $('#userEdu').val();//学历
                var userMajor = $('#userMajor').val();//专业
                var userMobile = $('#userMobile').val();//手机
                var userTel = $('#userTel').val();//电话
                var userQq = $('#userQq').val();//QQ
                
                
                if('' == $.trim(userLnm)) {
                    alert('请输入用户名！');
                    return;
                } else if (userLnm.match(reUserName) == null){
                    alert("用户名 请输入数字和字母，长度在4到15个！");
                    return;
                }
                if('' == $.trim(userPass)) {
                    alert('请输入密码!');
                    return;
                } else if (userPass.match(rePass) == null){
                    alert("密码 请输入数字和字母，长度在6到20个！");
                    return;
                }
                if('' == $.trim(userRnm)) {
                    alert('请输入学员姓名!');
                    return;
                } else if (userRnm.match(reName) == null){
                    alert("学员姓名 请输入汉字，长度在2到10个！");
                    return;
                }
                if('' == $.trim(userEmail)) {
                    alert('请输入注册邮箱!');
                    return;
                } else if(userEmail.match(reMail) == null){
                    
                    alert("请输入正确的邮箱!");
                    return;
                }
                if('' == $.trim(userMale)) {
                    alert('请选择性别!');
                    return;
                }
                
                if('' != $.trim(userTel) && userTel.match(reTel) == null) {
                    alert('请输入正确的电话号码！');
                    return;
                }
                
                if('' != $.trim(userMobile) && userMobile.match(reMobile) == null) {
                    alert('请输入正确的手机号！');
                    return;
                }
                
                if('' != $.trim(userQq) && userQq.match(reQQ) == null) {
                    alert('请输入正确的QQ号！');
                    return;
                }
                
                $.ajax({
                    type: "POST",
                      url: contextRootPath + "/stuControl/addStu",
                      contentType: "application/json; charset=utf-8",  
                      data:JSON.stringify({"userLnm": userLnm, "userPass": userPass ,"userRnm": userRnm,"userTel": userTel, "userEmail":userEmail,"userMobile":userMobile,"userQq":userQq}),
                      dataType: "html",
                      success: function(data){
                      /** 成功构造内容**/
                          alert("添加成功");
                          $("input").val('');
                      }
                });
             });
        },
        
        /**
         * 选择课程 全选按钮点击
         * @param box
         */
        checkbox : function (box){
            if($(box).is(':checked')){
                $("input[name='checkboxCour']").prop("checked",true);
            } else {
                $("input[name='checkboxCour']").prop("checked",false);
            }
        },
        
        
        /**
         * 选择课程 list下面确定按钮点击
         * @param box
         */
        checkboxCour : function (box){
            var ht = '';
            
            $("input[name='checkboxCour']").each(function(){
                if($(this).is(':checked')) {
                    
                    var name = $(this).parent('td').find('span').text();
                    var courID = $(this).val();
                    for (var i = 0; i < stuMngEvent.courids.length;i++) {
                        if(courID == stuMngEvent.courids[i]) {
                            return;
                        }
                    }
                    
                    stuMngEvent.courids.push(courID);

                    stuMngEvent.cournames.push(name);
                    
                } else {
                    
                }
                
            });
            $('#yxzkcID div').empty();
            if(null != stuMngEvent.courids && stuMngEvent.courids.length > 0){
                for (var i = 0; i < stuMngEvent.courids.length;i++) {
                    ht += '<span>'+ stuMngEvent.cournames[i] + '&nbsp;&nbsp;<a href="#" onclick="stuMngEvent.clearCour(this);" id="'+stuMngEvent.courids[i]+'"><img src="../images/cha.png" /></a>&nbsp;&nbsp;</span>';
                }
                $('#yxzkcID div').append(ht);
            }
            
        },
        
        /**
         * 选择用户后 ,点击删除事件
         * @param thisObj
         */
        clearCour : function (thisObj) {
            for (var i = 0; i < stuMngEvent.courids.length;i++) {
                if($(thisObj).attr('id') == stuMngEvent.courids[i]) {
                    stuMngEvent.courids.splice(i,1);
                    stuMngEvent.cournames.splice(i,1);
                    break;
                }
            }
            $(thisObj).parent('span').detach();
        },
        
        /**
         * 查询课程
         */
        searchCour : function (){
                //$('#select02').show();
                
                if('' == $.trim($('#inputCK').val())) {
                    alert('请输入课程名称！');
                    return;
                } 
                var tp = $('#lbxz').val();
                $.ajax({
                    type: "POST",
                    url: contextRootPath + "/stuControl/popCourInit2",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({"courseNm": $.trim($('#inputCK').val()),"courseTp":tp }),
                    dataType: "html",
                    success: function(data){
                          /** 成功构造内容**/
                        $("#kclist").html(data);
                        $('#yxzkcID div').empty();
                        $('#select01').show();
                        stuMngEvent.courids = new Array();
                        stuMngEvent.cournames = new Array();
                    }
                });
        },
        /**
         * 选择课程 确定按钮
         */
        chooseCour :function(){
            
            $('#select01').hide();
            if(null != stuMngEvent.courids && stuMngEvent.courids.length > 0 && null != stuMngEvent.courids[0] && "" != stuMngEvent.courids[0] ){
                var ht = '';
                for (var i = 0; i < stuMngEvent.courids.length; i++) {
                    ht += '<span>&nbsp;&nbsp;' + stuMngEvent.cournames[i] + '&nbsp;&nbsp;<a href="#"  onclick="stuMngEvent.clearCourMain(this);" id="'+stuMngEvent.courids[i]+'"><img src="../images/cha.png" /></a>&nbsp;&nbsp;</span>';
                }
                
                stuMngEvent.courids_new = stuMngEvent.courids;
                stuMngEvent.cournames_new = stuMngEvent.cournames;
                
                $('#courslistID').append(ht);
            }
        },
        /**
         * 选择课程后 ,点击删除事件
         * @param thisObj
         */
        clearCourMain : function (thisObj) {
            for (var i = 0; i < stuMngEvent.courids_new.length;i++) {
                if($(thisObj).attr('id') == stuMngEvent.courids_new[i]) {
                    stuMngEvent.courids_new.splice(i,1);
                    stuMngEvent.cournames_new.splice(i,1);
                    break;
                }
            }
            $(thisObj).parent('span').detach();
        },
        
        /**
         * 选择用户后 ,点击删除事件
         * @param thisObj
         */
        clearStuMain : function (thisObj) {
            for (var i = 0; i < stuMngEvent.ids_new.length;i++) {
                if($(thisObj).attr('id') == stuMngEvent.ids_new[i]) {
                    stuMngEvent.ids_new.splice(i,1);
                    stuMngEvent.names_new.splice(i,1);
                    break;
                }
            }
            $(thisObj).parent('span').detach();
        },
        
        
        
        /**
         * 确认分配
         */
        save :function(){
            
            if(null == stuMngEvent.cournames_new || 0 == stuMngEvent.cournames_new.length || null == stuMngEvent.cournames_new[0] || '' == stuMngEvent.cournames_new[0] 
                || null == stuMngEvent.courids_new || 0 == stuMngEvent.courids_new.length || null == stuMngEvent.courids_new[0] || '' == stuMngEvent.courids_new[0]){
                
                alert("请选择课程");
                return;
            }
            if(null == stuMngEvent.ids_new || 0 == stuMngEvent.ids_new.length || null == stuMngEvent.ids_new[0] || '' == stuMngEvent.ids_new[0] 
                || null == stuMngEvent.names_new || 0 == stuMngEvent.names_new.length || null == stuMngEvent.names_new[0] || '' == stuMngEvent.names_new[0]){
                
                alert("请选择用户");
                return;
            }
            $.ajax({
                type: "POST",
                url: contextRootPath + "/stuControl/save",
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify({"userIds": stuMngEvent.ids_new,"courIds":stuMngEvent.courids_new}),
                dataType: "html",
                success: function(data){
                    alert(data);
                    alert("添加成功!");
                         /** 成功构造内容**/
                }
            });
        }
        
        
    }
});